package com.ys.nms.module.business.controller.admin.ipv4Mac;

import com.baomidou.dynamic.datasource.annotation.Slave;
import com.fhs.core.trans.anno.TransMethodResult;
import com.ys.nms.framework.tenant.core.aop.TenantIgnore;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.ipv4Mac.vo.*;
import com.ys.nms.module.business.dal.dataobject.ipv4Mac.Ipv4MacDO;
import com.ys.nms.module.business.service.ipv4Mac.Ipv4MacService;

@Tag(name = "管理后台 - IPMAC")
@RestController
@RequestMapping("/business/ipv4-mac")
@Validated
public class Ipv4MacController {

    @Resource
    private Ipv4MacService ipv4MacService;

    @PostMapping("/create")
    @Operation(summary = "创建IPMAC")
    public CommonResult<Long> createIpv4Mac(@Valid @RequestBody Ipv4MacSaveReqVO createReqVO) {
        return success(ipv4MacService.createIpv4Mac(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新IPMAC")
    public CommonResult<Boolean> updateIpv4Mac(@Valid @RequestBody Ipv4MacSaveReqVO updateReqVO) {
        ipv4MacService.updateIpv4Mac(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除IPMAC")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteIpv4Mac(@RequestParam("id") Long id) {
        ipv4MacService.deleteIpv4Mac(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除IPMAC")
    public CommonResult<Boolean> deleteIpv4MacList(@RequestParam("ids") List<Long> ids) {
        ipv4MacService.deleteIpv4MacListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得IPMAC")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Ipv4MacRespVO> getIpv4Mac(@RequestParam("id") Long id) {
        Ipv4MacDO ipv4Mac = ipv4MacService.getIpv4Mac(id);
        return success(BeanUtils.toBean(ipv4Mac, Ipv4MacRespVO.class));
    }

    @Slave
    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得IPMAC分页")
    public CommonResult<PageResult<Ipv4MacRespVO>> getIpv4MacPage(@Valid Ipv4MacPageReqVO pageReqVO) {
        PageResult<Ipv4MacDO> pageResult = ipv4MacService.getIpv4MacPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Ipv4MacRespVO.class));
    }


    @TenantIgnore
    @Slave
    @TransMethodResult
    @GetMapping("/list")
    @Operation(summary = "获得IPMAC")
    public CommonResult<List<Ipv4MacRespVO>> getIpv4MacList(Ipv4MacPageReqVO pageReqVO) {
        List<Ipv4MacDO> list = ipv4MacService.getIpv4MacList(pageReqVO);
        List<Ipv4MacRespVO> bean = BeanUtils.toBean(list, Ipv4MacRespVO.class);
        for (Ipv4MacRespVO respVO : bean) {

        }

        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出IPMAC Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIpv4MacExcel(@Valid Ipv4MacPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Ipv4MacDO> list = ipv4MacService.getIpv4MacPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "IPMAC.xls", "数据", Ipv4MacRespVO.class,
                        BeanUtils.toBean(list, Ipv4MacRespVO.class));
    }

}